<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class news_model extends MY_Model
{

	public function getNewsList($data = array(),$per_page, $limit){
        
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
        if(!empty($data['review_content'])){
            $where .= " AND r.content like '%{$data['review_content']}%'";
        }
        if(!empty($data['fromUsername'])){
            $where .= " AND r.fromUsername = '{$data['fromUsername']}'";
        }
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT n.*, u.username FROM tr_news n , tr_admin_user u WHERE n.isDel<>1 AND n.operationname=u.id  {$where}  ORDER BY n.addtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }


    public function getnewsCnt($data = array()){
        $where = " ";
        if(!empty($data['starttime'])){
            $where .= " AND p.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND p.createtime <= {$data['endtime']}";
        }
        if(!empty($data['content'])){
            $where .= " AND p.content like '%{$data['content']}%'";
        }
        if(!empty($data['review_content'])){
            $where .= " AND r.content like '%{$data['review_content']}%'";
        }
        if(!empty($data['fromUsername'])){
            $where .= " AND r.fromUsername = '{$data['fromUsername']}'";
        }
      
        $sql = "SELECT n.*, u.username FROM tr_news n , tr_admin_user u WHERE n.isDel=0 AND n.operationname=u.id  {$where}  ";
        $query = $this->db->query($sql);
        return $query->num_rows();
    }


     //编辑
    public function editNews($id,$data = array()){
        if($id){
            $this->db->where("newsID",$id);
            $res = $this->db->update("tr_news",$data);
        }else{
            $res = $this->db->insert("tr_news",$data);
        }
        return $res;
    }

    public function delNews($id){
        $this->db->where("newsID",$id);
        $data = array(
            'isDel'=>1
        );
        $res = $this->db->update("tr_news",$data);
        return $res;
    }

     //根据id获取广告信息
    public function getNewsInfobyID($id){
        $this->db->where("newsID",$id);
        $query = $this->db->get("tr_news");
        return $query->row_array();
    }


    public function editStatus($data = array()){

        $this->db->where("newsID",$data['newsID']);
        
        $data = array(
            'onlinestatus'=>$data['onlinestatus']
        );
       
        $res = $this->db->update("tr_news",$data);
        return $res;
    }

}


